from pathlib import Path
from datetime import datetime

rootFolder = Path("E:/GitProject/using-python-to-process-excel-u/first/销售表")

fileList = rootFolder.rglob("*.txt")
for i in fileList:
    if i.name.startswith("~$"):
        continue
    if i.is_dir():
        continue
    newFolder = rootFolder / i.stem
    if not newFolder.exists():
        newFolder.mkdir(parents=True)
    fileTime = i.stat().st_mtime
    fileTime = datetime.fromtimestamp(fileTime)
    fileTime = fileTime.strftime("%Y年")
    newFileName = fileTime + "-" + i.parent.stem + i.name
    newPath = newFolder / newFileName
    newPath = newPath.with_suffix(".csv")
    i.replace(newPath)
    folderList = rootFolder.iterdir()
    for j in folderList:
        if i.is_file():
            continue
        fc = len(list(j.iterdir()))
        if fc == 0:
            j.rmdir()

